<?php
/**
 * Form
 *
 * Form is created.
 * Request is processed.
 *
 * <code>
 * // Call of the form of a phtml file out
 * 
 * // Form is defined in the PHP file
 * <?php echo $this->action('php',
 *                           'load',
 *                           'form',
 *                           array('file' => 'contact')); ?>
 * // Form is defined in the XML file
 * <?php echo $this->action('xml',
 *                          'load',
 *                          'form',
 *                          array('file' => 'contact')); ?>
 * </code> 
 * 
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @package Form
 * @subpackage Form_LoadController
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >= 5.3
 * @todo Translate fertig stellen, Daten in der View ausgeben
 */

class Form_LoadController extends Zend_Controller_Action
{
    /**
     * Data to load the Translate File
     *
     * @var Zend_Translate
     */
    protected $_translate;
    /**
     * File name of the Form
     *
     * @var string
     */
    protected $_fileName;

    public function init()
    {
        $this->_helper->viewRenderer->setNoRender(true);

        // Comes in the next version
        /*
        $this->_translate = new Zend_Translate(
            array(
                'adapter' => 'ini',
                'content' => 'languages/form_de_DE.ini',
                'locale'  => 'de'
            )
        );
        */

        $this->_fileName = $this->getRequest()->file;
    }
    /**
     * Output of Forms with a XML File
     * 
     * @return void
     */
    public function xmlAction() {

        if(($formPath = $this->_path('xml'))) {
            $construction = new Zend_Config_Xml($formPath);
            $form =  new Zend_Form($construction);

            // Comes in the next version
            //$form->setTranslator($this->_translate);

            if ($this->getRequest()->isPost()) {
                
                if ($form->isValid($_POST)) {
                    $this->_buildView($form);
                } else {
                    echo $form;
                }
            } else {
                echo $form;
            }
        } else {
            echo "Formular fehlt!";
        }
    }
    /**
     * Output of Forms with a PHP File
     * 
     * @return void
     */
    public function phpAction() {

        if(($formPath = $this->_path('php'))) {

            $form = new Form_Form_Elements;
            include_once $formPath;
            
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($_POST)) {
                    $this->_buildView($form);
                } else {
                    echo $form->renderForm();
                }
            } else {
                echo $form->renderForm();
            }
        } else {
            echo "Formular fehlt!";
        }
    }
    /**
     * File Path Output
     * 
     * @param string $format
     * 
     * @return boolan or string 
     */
    protected function _path($format) {

        if ($format == 'xml' || $format == 'php' || $format == 'ini') {

            return realpath('application' . DIRECTORY_SEPARATOR .
                            'configs' . DIRECTORY_SEPARATOR .
                            'modules' . DIRECTORY_SEPARATOR .
                            'form' . DIRECTORY_SEPARATOR .
                            $this->_fileName . '.' . $format);
        } else {
            return FALSE;
        }
    }
    /**
     * Bild View and Email Output
     * 
     * @param Zend_Form $form
     * 
     * @return void
     */
    protected function _buildView($form) {

        $formDatas = $form->getValues();

        $formNewDatas = array();
        foreach ($formDatas as $key => $value) {
            foreach ($form->getElements() as $name => $element) {
                if ($key == $name) {
                    $formNewDatas[$element->getLabel()] = $value;
                }
            }
        }
        // Data for the mail delivery and
        // Thanks page to be loaded
        if(($configPath = $this->_path('ini'))) {

            $config = new Zend_Config_Ini($configPath, 'de');
            $this->view->headline = $config->page->headline;
            $this->view->body = $config->page->text;

            if (($this->view->dataHeadline = $config->page->data)) {
                $this->view->data = $formNewDatas;
            }
            $this->view->footer = $config->page->footer;

            if (Form_Model_Mail::send($config, $formNewDatas))
                $this->render('data');
        } else {
            // TODO: Outpu for User and Backend (LOG)
            echo 'Config Datei für das Formular fehlt!';
        }
    }
}